/*
 * HeapMain.cpp
 *
 *  Created on: 17-Nov-2009
 *      Author: swapnil
 */

#include "../DiskSpaceManager/Database.h"
#include <iostream>
#include <stdio.h>
#include <fstream>
#include<string.h>
#include <malloc.h>
#include <time.h>
#include "Heap.h"
#include <strings.h>


int main() {

	enum database{EXIT, CREATE, INSERT, DELETEDATA, UPDATEDATA, GETDATA, GETALLDATA};

	Heap hp;
	Status st = ok;
	char* data = "hello world";
	char* data1 = "this is swapnil mishra";
	char* data2 = "i m in iiitb";
	char* data3 = "doing M.Tech from here";
	char* data4 = "this place is kool";
	char* data5 = "new one";
	char* update = "updated data";
	char* rec;
	PageId pid;
	int input = -1;
	printf("********************************************Menu for the Database******************************************************************\n\n\n");
	printf("Press the numbers and then enter to do the operations\n\n");
	printf("0 - Exit Program\n");
	printf("1 - Create Database\n");
	printf("2 - Insert Data\n");
	printf("3 - Delete Data\n");
	printf("4 - Update Data\n");
	printf("5 - Get Data\n");
	printf("6 - Get All Data\n\n");
	printf("Enter the choice : ");
	scanf("%d",&input);

while(true){

	switch(input){

	case EXIT:
		cout << "Program Terminated\n";
		return 0;

	case CREATE:
		hp.initDb(0);
		hp.initFirstDpPage();
		hp.exitDb();
		break;

	case INSERT:

		hp.initDb(1);
		int count;
		printf("Enter the Number of records you want to enter in multiple of 6 :");
		scanf("%d",&count);
		for (int iter = 0; iter <count; iter++) {
				hp.insertRecord(data);
				hp.insertRecord(data1);
				hp.insertRecord(data2);
				hp.insertRecord(data3);
				hp.insertRecord(data4);
				hp.insertRecord(data5);
			}
		hp.exitDb();
	break;

	case DELETEDATA:
		hp.initDb(1);
		printf("\nEnter the Record Id of the record you want to delete ---->\n\n");
		printf("Enter Page Number: ");
		scanf("%d",&pid.pageno);
		printf("Enter Slot Number: ");
		scanf("%d",&pid.slotno);
		hp.deleteRecord(pid);
		hp.exitDb();
	break;

	case UPDATEDATA:
		hp.initDb(1);
		printf("\nEnter the Record Id of the record you want to update ---->\n\n");
		printf("Enter Page Number: ");
		scanf("%d",&pid.pageno);
		printf("Enter Slot Number: ");
		scanf("%d",&pid.slotno);
		hp.updateRecord(pid, update);
		hp.exitDb();
	break;

	case GETDATA:
		hp.initDb(1);
		printf("\nEnter the Record Id of the record you want to get ---->\n\n");
		printf("Enter Page Number: ");
		scanf("%d",&pid.pageno);
		printf("Enter Slot Number: ");
		scanf("%d",&pid.slotno);
		rec = hp.getRecord(pid);

		if(rec == NULL) cout << "no record exist at this page and slot \n";
		else if(rec != NULL){
			cout << "\n\nRecord found :\t";
			 for(unsigned int iter=0; iter<strlen(rec); iter++){
				 cout << *(rec + iter) ;
			 }
		}
		cout << "\n\n\n";
		hp.exitDb();
	break;

	case GETALLDATA:
		hp.initDb(1);
		hp.openScan();
		hp.exitDb();
	break;

	default:
		cout << "invalid input\n";
	break;
	};

	printf("0 - Exit Program\n");
	printf("1 - Create Database\n");
	printf("2 - Insert Data\n");
	printf("3 - Delete Data\n");
	printf("4 - Update Data\n");
	printf("5 - Get Data\n");
	printf("6 - Get All Data\n\n");
	printf("Enter the choice : ");
	scanf("%d",&input);
};

}


